<?php
require_once 'Dto.php';
class Dao {
    
    protected $_name;
    private $db;
    
    public function __construct($tablename = "", DataProvider $dbObj) {
        $this->db = $dbObj;
        if(!empty($tablename)) {
            $this->_name = $tablename;
        }
        $this->_init();
    }
    
    protected function _init() {
        
    }
    
    public function insert(Dto $dto) {
        $keys = array();
        $values = array();
        foreach($dto as $key=>$value) {
            if(!empty($value)) {
                $keys[] = "`".$key."`";
                $values[] = "'".$value."'";
            }
        }
        $strKeys = implode(',',$keys);
        $strValues = implode(',',$values);
        $query = "INSERT INTO {$this->_name}({$strKeys}) 
                  VALUES({$strValues})";
        $temp = $this->db->ExecuteQuery($query);
        if($temp) {
            $temp = $this->db->InsertId();
        }
        return $temp;
    }
    
    public function delete($where = "") {
        $query = "DELETE FROM {$this->_name}";
        if(!empty($where)) {
            $query = $query." WHERE {$where}";
        }
        return $this->db->ExecuteQuery($query);
    }
    
    public function update(Dto $dto, $where) {
        $set=array();
        $query = "";
        foreach($dto as $key=>$value) {
            if(isset($value)){
                $set[] = "{$key}='{$value}'";   
            }else{
                $set[] = "{$key}=NULL";
            }
        }
        $sets = implode(',', $set);
        if(!empty($set)) {
            $query = "UPDATE {$this->_name} 
                      SET {$sets} 
                      WHERE {$where}";
        }
        return $this->db->ExecuteQuery($query);
    }
    
    public function query($query) {        
        $result = $this->db->ExecuteQuery($query);
        $dtoArray = new ArrayObject();
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $dtoArray[] = new Dto($this->_name, $this->db, $row);
        }
        return $dtoArray;
    }
    
    public function select($param = array('fields'=>array(),'where'=>'','order'=>'','limit'=>'')) {
        $field = "*";
        if(!empty($param['fields'])) {
            $field = implode(',',$param['fields']);
        }
        $query = "SELECT {$field} FROM {$this->_name}";
        if(!empty($param['where'])) {
            $query = $query." WHERE {$param['where']}";
        }
        if(!empty($param['order'])){
            $query = $query." ORDER BY {$param['order']}";
        }
        if(!empty($param['limit'])) {
            $query = $query." LIMIT {$param['limit']}";
        }
        //echo $query;
        return $this->query($query);
    }
    
    public function countTotalRow(){
        $query  = "SELECT * FROM {$this->_name}";
        $result = $this->db->ExecuteQuery($query);
        return mysql_numrows($result); 
    }
    public function __destruct(){
        
    }
}
?>